package com.snape.service.impl;

import com.snape.dao.VideoDao;
import com.snape.entity.Video;
import com.snape.service.VideoService;
import com.snape.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by liuqingcan_snape on 2017/3/29.
 */
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    VideoDao videoDao;

    /**
     * 查询所有的课程详细信息
     * @param roomId
     * @return map
     */
    public Map<String,Object> getAllVideoInfoByRoomId(int roomId){
        Map<String,Object> result = new HashMap<String, Object>();

        try {
            ArrayList<Video> videos = videoDao.selectAllInfoByRoomId(roomId);
            //    这里应该针对数据库查询出来的结果做一下去重工作
            //    1.  "http://19.offcn.comjavascript:;"
            //    2. "第2讲"
            Map<String,String> filter = new HashMap<String, String>(64);
            ArrayList<Video> copyOne = new ArrayList<Video>();
            for (int index = 0;index < videos.size();index++){
                //2.
                if (filter.containsKey(videos.get(index).getRoundName())){
//                    videos.remove(index);
                }else {
                    copyOne.add(videos.get(index));
                    filter.put(videos.get(index).getRoundName(),"test");
                }
                //1.
//                if ()
            }

            Collections.sort(copyOne,comparator);


            result.put("statusCode", StatusUtils.SUCCESS.getStatusCode());
            result.put("data",copyOne);
            result.put("statusMsg",StatusUtils.SUCCESS.getStatusName());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("statusCode", StatusUtils.SELECT_DATA_ERROR.getStatusCode());
            result.put("data",null);
            result.put("statusMsg",StatusUtils.SELECT_DATA_ERROR.getStatusName());
        }

        return result;
    }


    public Comparator<Video> comparator = new Comparator<Video>() {
//        @Override
        public int compare(Video o1, Video o2){
            String v1 = o1.getRoundName();
            String v2 = o2.getRoundName();
            String x1 = v1.substring(1,2);
            String x2 = v2.substring(1,2);
            System.out.print(x1+x2);
            return x1.compareTo(x2);
        }
    };



}
